home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-01-02 | 4.6 KB | 168 lines | [TEXT/CWIE] |
- // ===========================================================================
- // CArgumentParser.cpp ©1996 Microsoft Corporation. All rights reserved.
- // ===========================================================================
-
- #include "CArgumentParser.h"
-
- CArgumentParser::CArgumentParser(void)
- {
- Buffer = ::NewHandle(0);
-
- Reset();
- }
-
- CArgumentParser::~CArgumentParser(void)
- {
- ::DisposeHandle(Buffer);
- }
-
- void CArgumentParser::Reset(void)
- {
- ::SetHandleSize(Buffer, 0);
- CurrentState = Stop;
- InputBufferCount = 0;
- }
-
- Boolean CArgumentParser::ProcessChar(char c)
- {
- //
- // parse tables
- //
- parseraction ActTable[] = { /* Alpha Numeric WhiteSpace Assignment Quote Unknown EOF */
- /* WaitArgName */ AddToBuffer, Error, None, Error, Error, Error, None,
- /* BuildArgName */ AddToBuffer, AddToBuffer, TermArgName, TermArgName, Error, Error, Error,
- /* WaitAssign */ Error, Error, None, None, Error, Error, Error,
- /* WaitArgValue */ AddToBuffer, AddToBuffer, None, Error, None, Error, Error,
- /* BuildArgVal */ AddToBuffer, AddToBuffer, TermArgValue, Error, Error, Error, TermArgValue,
- /* BuildArgValLit */ AddToBuffer, AddToBuffer, AddToBuffer, AddToBuffer, TermArgValue, AddToBuffer, Error,
- /* Undefined */ None, None, None, None, None, None, None
- };
-
- parserstate StateTable[] = {/* Alpha Numeric WhiteSpace Assignment Quote Unknown EOF */
- /* WaitArgName */ BuildArgName, Undefined, WaitArgName, Undefined, Undefined, Undefined, Stop,
- /* BuildArgName */ BuildArgName, BuildArgName, WaitAssign, WaitArgValue, Undefined, Undefined, Undefined,
- /* WaitAssign */ Undefined, Undefined, WaitAssign, WaitArgValue, Undefined, Undefined, Undefined,
- /* WaitArgValue */ BuildArgVal, BuildArgVal, WaitArgValue, Undefined, BuildArgValLit, Undefined, Undefined,
- /* BuildArgVal */ BuildArgVal, BuildArgVal, WaitArgName, Undefined, Undefined, Undefined, Stop,
- /* BuildArgValLit */ BuildArgValLit, BuildArgValLit, BuildArgValLit, BuildArgValLit, WaitArgName, BuildArgValLit, Undefined,
- /* Undefined */ Undefined, Undefined, Undefined, Undefined, Undefined, Undefined, Undefined
- };
-
- Int16 Index;
-
- // find the index into the table based upon the input character's type
- {
- chartype t;
-
- if ((c >= 'a' && c <='z') || (c >= 'A' && c <='Z'))
- t = Alpha;
- else if (c >= '0' && c <= '9')
- t = Numeric;
- else if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
- t = WhiteSpace;
- else if (c == '=')
- t = Assignment;
- else if (c == '\"')
- t = Quote;
- else if (c == '\0')
- t = EOF;
- else
- t = Unknown;
-
- Index = CurrentState * CharTypeCount + t;
- }
-
- // do the action in the table and go to the next state
- CurrentState = StateTable[Index];
- switch (ActTable[Index])
- {
- case AddToBuffer:
- *(InputBuffer + InputBufferCount++) = c;
- break;
- case TermArgName:
- case TermArgValue:
- {
- unsigned long TotalBufferSize = ::GetHandleSize(Buffer);
- *(InputBuffer + InputBufferCount++) = '\0';
- ::SetHandleSize(Buffer,TotalBufferSize + InputBufferCount);
- ::BlockMove(InputBuffer, *Buffer + TotalBufferSize, InputBufferCount);
- InputBufferCount = 0;
- }
- break;
- case None:
- break;
- case Error:
- default:
- break;
- }
-
- return CurrentState != Undefined;
- }
-
-
- Boolean CArgumentParser::GetArguments(Ptr *pArgBuffer, char **pArgNames[], char **pArgValues[], short *pArgCount)
- {
- Boolean ReturnValue = true;
-
- if (CurrentState == Stop)
- {
- Int32 BufferSize = ::GetHandleSize(Buffer);
- Ptr ArgBuffer;
- Int16 ArgCount;
-
- // figure out how many arguments we have
- {
- Int16 i;
- char *p;
-
- for (i = 0, p = *Buffer + BufferSize; p >= *Buffer; p--)
- if (*p == '\0')
- i++;
-
- ArgCount = i / 2;
- }
-
- // allocate a buffer to hold all of our data
- if ((ArgBuffer = ::NewPtr(BufferSize + ArgCount * 2 * sizeof(char *))) != NULL)
- {
- char *here = ArgBuffer + ArgCount * 2 * sizeof(char *);
- char **ArgNames, **ArgValues;
- Int16 i = ArgCount;
-
- ::BlockMove(*Buffer, ArgBuffer + ArgCount * 2 * sizeof(char *), BufferSize);
- ArgNames = (char **)ArgBuffer;
- ArgValues = (char **)ArgBuffer + ArgCount;
-
- while (i--)
- {
- *ArgNames++ = here;
- while (*here++ != '\0')
- ;
- *ArgValues++ = here;
- while (*here++ != '\0')
- ;
- }
-
- // transfer out the pointers
- *pArgBuffer = ArgBuffer;
- *pArgCount = ArgCount;
- *pArgNames = (char **)ArgBuffer;
- *pArgValues = (char **)ArgBuffer + ArgCount;
- }
- else
- ReturnValue = false;
- }
- else
- ReturnValue = false;
-
- if (!ReturnValue)
- {
- *pArgBuffer = NULL;
- *pArgCount = 0;
- *pArgNames = NULL;
- *pArgValues = NULL;
- }
-
- return ReturnValue;
- }
-